package com.tiandang.controller;

import net.sf.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tiandang.core.config.AuthResource;
import tiandang.core.model.TemplateData;
import tiandang.core.response.CommonReturnType;
import tiandang.core.service.WXService;
import tiandang.core.utils.WX_TemplateMsgUtil;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/test")
public class TestController {

  @Autowired
  private WXService wxService;

  @Autowired
  private AuthResource resource;

  @GetMapping("/push")
  public CommonReturnType push() {
    String touser = "ox8t75QkiUS41ew0P24awrOemKrY"; //被推送人的openid

    AuthResource bean = new AuthResource();
    BeanUtils.copyProperties(resource, bean);
    String templateId = bean.getTemplateId();

    String page = "index?managerId=2";
    String formid = "5bbb8518db1040b8bd5c03140e7b03a4";

    Map<String, TemplateData> param = new HashMap<>();
    param.put("keyword1", new TemplateData("张鑫的团购"));
    param.put("keyword2", new TemplateData("测试商品"));
    param.put("keyword3", new TemplateData("200"));
    param.put("keyword4", new TemplateData("张鑫"));
    param.put("keyword5", new TemplateData("2019-03-29 09:00:00"));
    param.put("keyword6", new TemplateData("2019-03-30 20:00:00"));
    param.put("keyword7", new TemplateData("12小时08分"));

    JSONObject jsonObject = JSONObject.fromObject(param);

    String accessToken = wxService.getAccessToken();

    String emphasisKeyword = "keyword1.DATA";

    String result = WX_TemplateMsgUtil.sendWechatMsgToUser(touser, templateId, page, formid, jsonObject, emphasisKeyword,accessToken);
    if (result.equals("success")) {
      //用过之后删除
      wxService.deleteFormid(formid);
    }
    return CommonReturnType.success();
  }

}